/*
 * Copyright (C) 2012 "Lexaden.com"
 *     contact@lexaden.com [http://www.lexaden.com]
 *
 *     This file is part of Lexaden Administration.
 *
 *     Lexaden Administration is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.navajo.web.spring;

import java.io.Serializable;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;

import com.vaadin.server.VaadinSession;

/**
 * This MessageSource caches both the accessed ResourceBundle instances and
 * the generated MessageFormats for each message. It also implements rendering of
 * no-arg messages without MessageFormat, as supported by the AbstractMessageSource
 * base class. The caching provided by this MessageSource is significantly faster
 * than the built-in caching of the <code>java.util.ResourceBundle</code> class.
 */
public class DefaultMessageSource extends ResourceBundleMessageSource implements Serializable,
         com.navajo.web.spring.MessageSource {
    private static final Log LOGGER = LogFactory.getLog(DefaultMessageSource.class);

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key) {
        return getMessage(key, getLocale());
    }

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key, Locale locale) {
        String message = super.resolveCodeWithoutArguments(key, locale);
        if (message == null) {
            logError(key, locale);
            return getMissingKeyFormat(key);
        }

        return message;
    }

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key, Locale locale, Object... args) {
        try {
            return getMessage(key, args, getLocale());
        } catch (NoSuchMessageException nsme) {
            logError(key, locale);
            return getMissingKeyFormat(key);
        }
    }

    /**
     * {@inheritDoc}
     */
    public String getMessage(String key, Object... args) {
        return getMessage(key, getLocale(), args);
    }

    private Locale getLocale() {
    	System.out.println(VaadinSession.getCurrent().getLocale());
    	
    	return VaadinSession.getCurrent().getLocale();
    	
        
    }

    /**
     * {@inheritDoc}
     */
    public String getMessageOrNull(String key, Object... args) {
        return getMessageInternal(key, args, getLocale());
    }

    public static String getMissingKeyFormat(String key) {
        return "[" + key + "]";
    }

    private void logError(String key, Locale locale) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("No key found for :" + key + " and Locale:" + locale);
        }
    }

}
